/**
 * @author zhoushengtao
 * @since 2012-8-27 下午9:00:32
 */

package com.stchou.superbrowser.activity;

import com.stchou.superbrowser.ui.widget.SuperImageButton;
import com.stchou.superbrowser.ui.widget.SuperWebview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ProgressBar;

public class WebviewActivity extends Activity implements OnClickListener, OnTouchListener
{
    private SuperWebview m_webView;
    private ProgressBar m_progressBar;
    public static final String BAIDU_GATE = "http://gate.baidu.com/tc?from=opentc&src=";
    // nav bar
    private SuperImageButton m_bnt1;
    private SuperImageButton m_bnt2;
    private SuperImageButton m_menuButton;
    private SuperImageButton m_tabButton;
    private SuperImageButton m_homeButton;
    // title bar
    private EditText m_webSiteText;
    private EditText m_searchText;

    
    public static String URL = "m.hao123.com";
    
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        initView();
    }

    /**
     * 初始化 views
     */
    private void initView()
    {
        // /webview
        m_webView = (SuperWebview)findViewById(R.id.superWebview);
        m_webView.setProgressBar(m_progressBar);
        m_webView.setStopButton(m_bnt2);
        m_webView.requestFocus();
        if (!URL.equals(""))
            m_webView.loadUrl(URL);

        m_progressBar = (ProgressBar)findViewById(R.id.progress);

        m_bnt1 = (SuperImageButton)findViewById(R.id.superImageButton1);
        m_bnt1.setOnClickListener(this);

        m_bnt2 = (SuperImageButton)findViewById(R.id.superImageButton2);
        m_bnt2.setOnClickListener(this);

        m_menuButton = (SuperImageButton)findViewById(R.id.superImageButton3);
        m_menuButton.setOnClickListener(this);

        m_tabButton = (SuperImageButton)findViewById(R.id.superImageButton4);
        m_tabButton.setOnClickListener(this);

        m_homeButton = (SuperImageButton)findViewById(R.id.superImageButton5);
        m_homeButton.setOnClickListener(this);

        m_webSiteText = (EditText)findViewById(R.id.website_text);
        m_webSiteText.setOnTouchListener(this);

        m_searchText = (EditText)findViewById(R.id.search_text);
        m_searchText.setOnTouchListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.superImageButton1:
                m_webView.goBack();
                break;
            case R.id.superImageButton2:
                if (m_progressBar.getVisibility() == View.GONE)
                    m_webView.goForward();
                else
                {
                    m_webView.stopLoading();
                    m_bnt2.setImageResource(R.drawable.toolbar_stop_normal);
                    m_progressBar.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if ((event.getAction() == MotionEvent.ACTION_CANCEL))
            switch (v.getId())
            {
                case R.id.website_text:
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        Intent intent = new Intent(WebviewActivity.this, InputWebsiteActivity.class);
                        startActivity(intent);
                    }
                    break;
                case R.id.search_text:
                    Intent intent = new Intent(WebviewActivity.this, SearchActivity.class);
                    startActivity(intent);
                    break;
            }
        return false;
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (URL != "")
        {
            m_webView.requestFocus();
            m_webView.loadUrl(URL);
            URL = "";
        }
    }
}
